/*
	100% free public domain implementation of the SHA-1
	algorithm by Dominik Reichl <Dominik.Reichl@tiscali.de>

	* modified by Trevor Hogan for use with BNBT *

	=== Test Vectors (from FIPS PUB 180-1) ===

	"abc"
		A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D

	"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
		84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1

	A million repetitions of "a"
		34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
*/

#ifndef __KTVSHA1_H__
#define __KTVSHA1_H__

	//
	// SOLARIS USERS - IF YOUR SYSTEM IS LITTLE ENDIAN, REMOVE THE NEXT 3 LINES
	//  also see bnbt.h
	//

	#if defined( __APPLE__ ) || defined( __SOLARIS__ )
		#define BNBT_BIG_ENDIAN
	#endif

	#define MAX_FILE_READ_BUFFER 8000

	class CKtvSha1:public CKtvObject{
	public:
		// Rotate x bits to the left
		#define ROL32(value, bits) (((value)<<(bits))|((value)>>(32-(bits))))

		#ifdef BNBT_BIG_ENDIAN
			#define SHABLK0(i) (block->l[i])
		#else
			#define SHABLK0(i) (block->l[i] = (ROL32(block->l[i],24) & 0xFF00FF00) \
				| (ROL32(block->l[i],8) & 0x00FF00FF))
		#endif

		#define SHABLK(i) (block->l[i&15] = ROL32(block->l[(i+13)&15] ^ block->l[(i+8)&15] \
			^ block->l[(i+2)&15] ^ block->l[i&15],1))

		// SHA-1 rounds
		#define KTV_SHA_R0(v,w,x,y,z,i) { z+=((w&(x^y))^y)+SHABLK0(i)+0x5A827999+ROL32(v,5); w=ROL32(w,30); }
		#define KTV_SHA_R1(v,w,x,y,z,i) { z+=((w&(x^y))^y)+SHABLK(i)+0x5A827999+ROL32(v,5); w=ROL32(w,30); }
		#define KTV_SHA_R2(v,w,x,y,z,i) { z+=(w^x^y)+SHABLK(i)+0x6ED9EBA1+ROL32(v,5); w=ROL32(w,30); }
		#define KTV_SHA_R3(v,w,x,y,z,i) { z+=(((w|x)&y)|(w&x))+SHABLK(i)+0x8F1BBCDC+ROL32(v,5); w=ROL32(w,30); }
		#define KTV_SHA_R4(v,w,x,y,z,i) { z+=(w^x^y)+SHABLK(i)+0xCA62C1D6+ROL32(v,5); w=ROL32(w,30); }

		typedef union {
			unsigned char c[64];
			unsigned long l[16];
		} SHA1_WORKSPACE_BLOCK;

		// Two different formats for ReportHash(...)
		enum { REPORT_HEX = 0, REPORT_DIGIT = 1 };
		
	public:
		// Constructor and Destructor
		CKtvSha1();
		virtual ~CKtvSha1();

		unsigned long m_state[5];
		unsigned long m_count[2];
		unsigned char m_buffer[64];
		unsigned char m_digest[20];

		void Reset();

		// Update the hash value
		void Update(const unsigned char* data, unsigned int len);

		// Finalize hash and report
		void Final();
		void ReportHash(char *szReport, unsigned char uReportType = REPORT_HEX);
		void GetHash(unsigned char *uDest);

	private:
		// Private SHA-1 transformation
		void Transform(unsigned long state[5], const unsigned char buffer[64]);
	};

#endif // #ifndef __KTVSHA1_H__

